package com.betterjr.modules.loan.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.modules.acceptbill.entity.ScfAcceptBillDO;
import com.betterjr.modules.acceptbill.service.ScfAcceptBillDOService;
import com.betterjr.modules.credit.service.ScfCreditService;
import com.betterjr.modules.ledger.service.ContractLedgerService;
import com.betterjr.modules.loan.entity.ScfAssetInformation;
import com.betterjr.modules.order.entity.ScfInvoiceDO;
import com.betterjr.modules.order.entity.ScfOrderDO;
import com.betterjr.modules.order.service.ScfInvoiceDOService;
import com.betterjr.modules.order.service.ScfOrderDOService;
import com.betterjr.modules.receivable.entity.ScfReceivableDO;
import com.betterjr.modules.receivable.service.ScfReceivableDOService;

/** 
 * @ClassName: AssetInformationService 
 * @Description: 统计供应商资产，授信额度，金额信息
 * @author xuzx
 * @date 2017年11月16日 上午9:12:19 
 *  
 */
@Service
public class ScfAssetInformationService {
    @Resource
    private ContractLedgerService contractLedgerService;

    @Resource
    private ScfInvoiceDOService scfInvoiceDOService;

    @Resource
    private ScfReceivableDOService scfReceivableDOService;

    @Resource
    private ScfAcceptBillDOService scfAcceptBillDOService;

    @Resource
    private ScfOrderDOService scfOrderDOService;

    @Resource
    private ScfRequestService scfRequestService;

    @Resource
    private ScfCreditService scfCreditService;

    public ScfAssetInformation queryAllAssetInformation(final Map<String, Object> anParam, final Long anCustNo) {
        final ScfAssetInformation scfAssetInformation = new ScfAssetInformation();

        Map<String, Object> anMap = Collections3.filterMap(anParam, new String[] { "custNo" });
        // 供应商名称
        scfAssetInformation.setSupplierName(UserUtils.getDefCustInfo().getCustName());
        // 授信额度区域
        scfAssetInformation.setScfCreditMap(scfCreditService.findCreditSumByCustNo(anCustNo));
        // 设置金额区域
        scfAssetInformation.setSecuredLoanMap(scfRequestService.queryCustAmountArea(anMap, anCustNo));
        // 设置未审核贸易合同笔数
        scfAssetInformation.setNotAuditedContractLedgerCount(contractLedgerService.queryNotAuditedCount());
        // 设置未审核发票笔数
        scfAssetInformation.setNotAuditedscfInvoiceDOCount(
                (int) scfInvoiceDOService.queryIneffectiveInvoice(anMap, "1", 1, 10, true).getTotal());
        // 设置贸易合同统计信息，贸易合同笔数
        scfAssetInformation.setContractLedgerCount(contractLedgerService.queryContractLedgerCount());
        // 设置商业发票统计信息
        final ScfAssetInformation invoiceAssetInformation = queryInvoice(anCustNo);
        scfAssetInformation.setScfInvoiceDOCount(invoiceAssetInformation.getScfInvoiceDOCount());
        scfAssetInformation.setScfInvoiceDOMoney(invoiceAssetInformation.getScfInvoiceDOMoney());
        // 设置应收账款统计信息
        final ScfAssetInformation receivableInformation = queryReceivable(anMap);
        scfAssetInformation.setScfReceivableDOCount(receivableInformation.getScfReceivableDOCount());
        scfAssetInformation.setScfReceivableDOMoney(receivableInformation.getScfReceivableDOMoney());
        // 设置票据统计信息
        final ScfAssetInformation acceptBillDOInformation = queryAcceptBillDO(anMap);
        scfAssetInformation.setScfAcceptBillDOCount(acceptBillDOInformation.getScfAcceptBillDOCount());
        scfAssetInformation.setScfAcceptBillDOMoney(acceptBillDOInformation.getScfAcceptBillDOMoney());
        // 设置订单统计信息
        final ScfAssetInformation scfOrderDOInformation = queryScfOrderDO(anMap);
        scfAssetInformation.setScfOrderDOCount(scfOrderDOInformation.getScfOrderDOCount());
        scfAssetInformation.setScfOrderDOOMoney(scfOrderDOInformation.getScfOrderDOOMoney());
        return scfAssetInformation;

    }

    /**
     * 
     * @Title: queryInvoice 
     * @Description: 供应商查看登记，生效，未使用的发票 
     * @param anCustNo 供应商编号
     * @return 笔数，总额设置到对象
     * @throws 
     * @author xuzx
     * @date 2017年11月16日 上午11:11:58
     */
    private ScfAssetInformation queryInvoice(final Long anCustNo) {
        final ScfAssetInformation information = new ScfAssetInformation();
        List<ScfInvoiceDO> list = scfInvoiceDOService.queryInvoiceInformation(anCustNo);
        information.setScfInvoiceDOCount(list.size());
        information.setScfInvoiceDOMoney(queryscfInvoiceDOMoney(list));
        return information;

    }

    /**
     * 
     * @Title: queryscfInvoiceDOMoney 
     * @Description: 供应商登记，生效，未使用的发票金额
     * @param list  供应商登记，生效，未使用的发票列表
     * @return 供应商登记，生效，未使用的发票金额
     * @throws 
     * @author xuzx
     * @date 2017年11月16日 上午11:11:41
     */
    private BigDecimal queryscfInvoiceDOMoney(final List<ScfInvoiceDO> list) {

        BigDecimal totalMoney = new BigDecimal(0);
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            totalMoney = list.get(i).getBalance();
            if (totalMoney != null) {
                sum = totalMoney.add(sum);
            }
        }
        return sum;
    }

    /**
     * 
     * @Title: queryReceivable 
     * @Description: 供应商查看应收账款
     * @param anCustNo 供应商编号
     * @return 笔数，总额设置到对象
     * @throws 
     * @author xuzx
     * @date 2017年11月16日 下午1:53:24
     */
    private ScfAssetInformation queryReceivable(final Map<String, Object> anMap) {
        final ScfAssetInformation information = new ScfAssetInformation();
        List<ScfReceivableDO> list = scfReceivableDOService.queryNotUserReceivableList(anMap, true);
        information.setScfReceivableDOCount(list.size());
        information.setScfReceivableDOMoney(queryReceivableMoney(list));
        return information;

    }

    /**
     * 
     * @Title: queryReceivableMoney 
     * @Description: 统计经销商应收账款总额
     * @param list 供应商编号
     * @return 经销商应收账款总额
     * @throws 
     * @author xuzx
     * @date 2017年11月16日 下午1:53:30
     */
    private BigDecimal queryReceivableMoney(final List<ScfReceivableDO> list) {

        BigDecimal totalMoney = new BigDecimal(0);
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            totalMoney = list.get(i).getBalance();
            if (totalMoney != null) {
                sum = totalMoney.add(sum);
            }
        }
        return sum;
    }

    /**
     * 
     * @Title: queryAcceptBillDO 
     * @Description: 供应商查看生效，未使用票据
     * @param anCustNo 供应商编号
     * @return  笔数，总额设置到对象
     * @throws 
     * @author xuzx
     * @date 2017年11月16日 下午2:37:20
     */
    private ScfAssetInformation queryAcceptBillDO(final Map<String, Object> anMap) {
        final ScfAssetInformation information = new ScfAssetInformation();
        List<ScfAcceptBillDO> list = scfAcceptBillDOService.queryNotUserBillList(anMap, true);
        information.setScfAcceptBillDOCount(list.size());
        information.setScfAcceptBillDOMoney(queryAcceptBillDOMoney(list));
        return information;
    }

    /**
     * 
     * @Title: queryAcceptBillDOMoney 
     * @Description:供应商查看生效，未使用票据金额
     * @param list 供应商查看生效，未使用票据列表
     * @return 计算生效，未使用票据总额
     * @throws 
     * @author xuzx
     * @date 2017年11月16日 下午2:38:13
     */
    private BigDecimal queryAcceptBillDOMoney(final List<ScfAcceptBillDO> list) {
        BigDecimal totalMoney = new BigDecimal(0);
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            totalMoney = list.get(i).getBalance();
            if (totalMoney != null) {
                sum = totalMoney.add(sum);
            }
        }
        return sum;
    }

    /**
     * 
     * @Title: queryScfOrderDO 
     * @Description: 设置订单生效未使用笔数，总额
     * @param anCustNo 供应商编号
     * @return 笔数，总额设置到对象
     * @throws 
     * @author xuzx
     * @date 2017年11月16日 下午3:01:47
     */
    private ScfAssetInformation queryScfOrderDO(final Map<String, Object> anMap) {
        final ScfAssetInformation information = new ScfAssetInformation();
        List<ScfOrderDO> list = scfOrderDOService.queryNotUserOrderList(anMap, true);
        information.setScfOrderDOCount(list.size());
        information.setScfOrderDOOMoney(queryScfOrderDOMoney(list));
        return information;

    }

    /**
     * 
     * @Title: queryScfOrderDOMoney 
     * @Description: 供应商统计生效未使用订单金额
     * @param list 供应商统计生效未使用订单列表
     * @return 生效未使用订单金额总数
     * @throws 
     * @author xuzx
     * @date 2017年11月16日 下午3:04:59
     */
    private BigDecimal queryScfOrderDOMoney(final List<ScfOrderDO> list) {
        BigDecimal totalMoney = new BigDecimal(0);
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            totalMoney = list.get(i).getBalance();
            if (totalMoney != null) {
                sum = totalMoney.add(sum);
            }
        }
        return sum;
    }
}
